﻿@{
    var plugName = @admin.cyt369.com.Models.PlugName.Name;
    var tableName = PageViewModel.PageTableID(this, plugName);
    var tableToolBar = PageViewModel.PageTableToolbar(this, plugName);
    var menu_table_load_success = PageViewModel.ControlID(this, "menu_table_load_success", plugName);

    var menu_module_list_id = PageViewModel.ControlID(this, "menu_module_list", plugName);
    var menu_operation_list_id = PageViewModel.ControlID(this, "menu_operation_list", plugName);

    var on_selet_menu = PageViewModel.PageFunctionName(this, "on_selet_menu", plugName);

    var editFunName = PageViewModel.PageFunctionName(this, "edit", plugName);
    var editDlgName = PageViewModel.PageEditDialog(this, plugName);
    var saveEditFunName = PageViewModel.PageFunctionName(this, "save_edit", plugName);

    var deleteFunName = PageViewModel.PageFunctionName(this, "delete", plugName);

}


<script type="text/javascript">
    function @editFunName (id) {
        var module = $("#@menu_module_list_id").datalist("getSelected");
        if (module != null) {
            var dlg = $dlg('@editDlgName');
            dlg.dialog({
                title: '菜单编辑', width: 800, height: 480, closed: false, cache: false, modal: true, maximizable: false, resizable: false, maximized: false,
                href: '~/Systems/_EditAppMenu?MID='+module.Id+'&ID=' + id,
                onResize: function () { $(this).dialog('center'); },
                buttons: [
                    {
                        text: "保 存",
                        handler: function () { @saveEditFunName (); },
                        iconCls: "icon-save"
                    }, {
                        text: "取 消",
                        handler: function () { $("#@editDlgName").dialog("close"); },
                        iconCls: "icon-cancel"
                    }
                ],
                onLoad: function () {
                    LoadObject(id, "~/Systems/GetAppMenuByID/", function (menu) {
                        var form = $("#@editDlgName").find("form");
                        form.form("load", menu);
                        if (menu.Enable) {
                            form.find("#Enable").switchbutton("check");
                        } else {
                            form.find("#Enable").switchbutton("uncheck");
                        }
                        if (menu.CanFold) {
                            form.find("#CanFold").switchbutton("check");
                        } else {
                            form.find("#CanFold").switchbutton("uncheck");
                        }
                        if (menu.Expanded) {
                            form.find("#Expanded").switchbutton("check");
                        } else {
                            form.find("#Expanded").switchbutton("uncheck");
                        }
                    });
                }
            });
        } else {
            $.messager.alert('提示', '请先选择模块', 'info');
        }
    }

    function @saveEditFunName () {
        var form = $("#@editDlgName").find("form");
        if (form.form('enableValidation').form('validate')) {
            var menu = form.form_to_json(function (form, menu) {
                menu.Enable = form.find("#Enable").switchbutton("options").checked;
                menu.CanFold = form.find("#CanFold").switchbutton("options").checked;
                menu.Expanded = form.find("#Expanded").switchbutton("options").checked;
            });

            $.ajax({
                url: "~/Systems/SaveAppMenu",
                type: "POST",
                data: menu,
                success: function (e) {
                    if (e.Result) {
                        $.messager.alert('保存成功', '保存成功!', 'info', function () {
                            $('#@editDlgName').dialog('close');
                            var savedMenu = JSON.parse(e.Msg);
                            if (parseInt(menu.Id) == savedMenu.Id) {
                                var pp = $('#@tableName').treegrid('search', function (n) {
                                    return n.Id == parseInt(menu.Id);
                                });

                                if (pp != null) {
                                    if (pp.ParentID == savedMenu.ParentID) {
                                        $('#@tableName').treegrid('update', {
                                            id: savedMenu.Id,
                                            row: savedMenu
                                        });
                                    } else {
                                        $('#@tableName').treegrid('remove', pp.Id);
                                        pp = $('#@tableName').treegrid('search', function (n) {
                                            return n.Id == savedMenu.ParentID;
                                        });
                                        if (pp != null) {
                                            $('#@tableName').treegrid('append', {
                                                parent: savedMenu.ParentID,
                                                data: [savedMenu]
                                            });
                                        } else {
                                            $('#@tableName').treegrid('append', {
                                                data: [savedMenu]
                                            });
                                        }
                                    }
                                }
                            } else {
                                var dd = $('#@tableName').treegrid('search', function (n) {
                                    return n.Id == savedMenu.ParentID;
                                });
                                if (dd != null) {
                                    $('#@tableName').treegrid('append', {
                                        parent: savedMenu.ParentID,
                                        data: [savedMenu]
                                    });
                                } else {
                                    $('#@tableName').treegrid('append', {
                                        data: [savedMenu]
                                    });
                                }
                            }
                        });
                    } else {
                        $.messager.alert('保存失败', '保存失败: ' + e.Msg, 'error');
                    }
                },
                error: function (e) {
                    ShowAjaxError(e);
                }
            });
        }
    }
</script>

<script type="text/javascript">
    function @deleteFunName (id){
        if (id > 0 && confirm("确认删除菜单信息?")) {
            $.ajax({
                async: false,
                method: "DELETE",
                url: $.Content("~/Systems/DeleteAppMenu/" + id),
                context: $("#@tableName"),
                success: function (e) {
                    if (e.Result) {
                        this.treegrid('remove', id);
                    } else {
                        $.messager.alert('删除数据错误', '删除失败: ' + e.Msg, 'error');
                    }
                },
                error: function (e1, e2, e3) {
                    ShowAjaxError(e1, "删除数据错误");
                    this.treegrid('reload');
                }
            });
        }
    }
</script>

<script type="text/javascript">
    function @on_selet_menu (row) {
        if (row != null) {
            $("#@menu_operation_list_id").datalist("reload", '~/Systems/GetSystemMenuOperations/' + row.Id);
        }
    }

    function @menu_table_load_success (row, data) {
        if (data.length > 0) {
            $("#@tableName").treegrid("select", data[0].Id);
        }
    }
</script>
<table id="@tableName" class="easyui-treegrid" style="width:100%;" rownumbers="true" fit="true" fitcolumns="true"
       data-options="striped:true,border:false,idField: 'Id',treeField: 'Name',toolbar:'#@tableToolBar',onSelect:@on_selet_menu,onLoadSuccess:@menu_table_load_success">
    <thead>
        <tr>
            <th data-options="field:'Code'" width="100">菜单编号</th>

            <th data-options="field:'_edit',align:'center',formatter:function(v,r,i){
                                    return GridRowEditFmt('@editFunName', r.Id);
                            }" width="50">修改</th>
            <th data-options="field:'_remove',align:'center',formatter:function(v,r,i){
                                if(r.ParentID &gt; 0) {
                                    return GridRowDeleteFmt('@deleteFunName', r.Id);
                                }
                            }" width="50">删除</th>
            <th data-options="field:'Name'" width="180">名称</th>
            <th data-options="field:'Description'" width="180">说明</th>
            <th data-options="field:'Url'" width="300">路径</th>
            <th data-options="field:'Enable',align:'center',formatter:$.CheckdFormat" width="80">启用</th>
            <th data-options="field:'Sequence',align:'center'" width="80">排序</th>
        </tr>
    </thead>
</table>
<div id="@tableToolBar" class="table-toolbar">
    <a href="javascript:@editFunName ()" class="easyui-linkbutton t-btn"><i class="fa fa-plus-circle" style="color:green"></i><span>新增</span></a>
</div>